from django.http import HttpResponse, JsonResponse
from django.shortcuts import render, redirect



def login(request):
    # 跳转到登录页面
    print("请求方式", request.method)
    print("查询参数", request.get)
    print("表单参数", request.POST)

    return render(request, "login.html")

def index(request):
    return render(request, "index.html")


def detail(request, pk):
    with open(r"C:\Users\29521\Pictures\微信图片_20240513123903.jpg", "rb") as f:
        return HttpResponse(f.read(), content_type="image/jpeg", headers={
            "Content-Disposition": "attachment;filename={pk}.jpg".format(pk=pk)
        })

def user(request):
    # 跳转到某一个指定的路由中、主要使用 重定向实现
    # 重定向跳转 需要满足 2个条件
    # 1.响应的状态码 是 301（永久性重定向）或者 302 （临时重定向）
    # 2.设置 响应的头信息、且必须包含 Location 头、值为要跳转的 路由地址
    # 3.设置 响应的头信息、且设置 Content-Length(内容长度)值为0

    # return HttpResponse(status=302, headers={"Location": "/", "Content-Length": 0})
    # return redirect(to="https://www.baidu.com")
    # return redirect(to="home")
    return redirect(to="user_detail", id=100)
def list(self, request, *args, **kwargs):
    queryset = self.get_queryset()
    # 对 查询的结果进行分页
    page = self.paginate_queryset(queryset)
    # 对 page 内容进行序列化操作
    serializer = self.get_serializer(page, many=True)
    # 将当前页码 对应的数据 传给 get_paginated_response
    return self.get_paginated_response(serializer.data)

def user_detail(request, id):
    from datetime import date

    user = {
        "id": 10,
        "name": "张三",
        "email": "zhangsan@qiku.com",
        "tel": "1345960449",
        "birth": date(2000, 10, 10)
    }

    # import json
    # 以 JSON 的格式返回 数据
    # return HttpResponse(json.dumps(user, ensure_ascii=False), content_type="application/json")

    return JsonResponse(user, json_dumps_params={"ensure_ascii": False})
